home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1WJJSCX (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  5.8 KB  |  218 lines

  1. package com.sun.java.swing.plaf.basic;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.Rectangle;
  5. import java.awt.event.MouseAdapter;
  6. import java.awt.event.MouseEvent;
  7. import java.awt.event.MouseMotionListener;
  8. import java.io.Serializable;
  9.  
  10. class BasicSliderUI$TrackListener extends MouseAdapter implements MouseMotionListener, Serializable {
  11.    // $FF: synthetic field
  12.    BasicSliderUI this$0;
  13.    protected transient int offset;
  14.    protected transient int currentMouseX;
  15.    protected transient int currentMouseY;
  16.  
  17.    public void mouseReleased(MouseEvent e) {
  18.       if (this.this$0.slider.isEnabled()) {
  19.          this.offset = 0;
  20.          this.this$0.scrollTimer.stop();
  21.          if (this.this$0.slider.getSnapToTicks()) {
  22.             BasicSliderUI.access$isDragging(this.this$0, false);
  23.             this.this$0.slider.setValueIsAdjusting(false);
  24.          } else {
  25.             this.this$0.slider.setValueIsAdjusting(false);
  26.             BasicSliderUI.access$isDragging(this.this$0, false);
  27.          }
  28.  
  29.       }
  30.    }
  31.  
  32.    public void mousePressed(MouseEvent e) {
  33.       if (this.this$0.slider.isEnabled()) {
  34.          this.currentMouseX = e.getX();
  35.          this.currentMouseY = e.getY();
  36.          this.this$0.slider.requestFocus();
  37.          if (this.this$0.getThumbRect().contains(this.currentMouseX, this.currentMouseY)) {
  38.             switch (this.this$0.slider.getOrientation()) {
  39.                case 0:
  40.                   this.offset = this.currentMouseX - this.this$0.getThumbRect().x;
  41.                   break;
  42.                case 1:
  43.                   this.offset = this.currentMouseY - this.this$0.getThumbRect().y;
  44.             }
  45.  
  46.             BasicSliderUI.access$isDragging(this.this$0, true);
  47.             this.this$0.slider.setValueIsAdjusting(true);
  48.          } else {
  49.             BasicSliderUI.access$isDragging(this.this$0, false);
  50.             this.this$0.slider.setValueIsAdjusting(true);
  51.             Dimension sbSize = this.this$0.slider.getSize();
  52.             int direction = 1;
  53.             switch (this.this$0.slider.getOrientation()) {
  54.                case 0:
  55.                   if (this.this$0.getThumbRect().isEmpty()) {
  56.                      int scrollbarCenter = sbSize.width / 2;
  57.                      if (!this.this$0.slider.getInverted()) {
  58.                         direction = this.currentMouseX < scrollbarCenter ? -1 : 1;
  59.                      } else {
  60.                         direction = this.currentMouseX < scrollbarCenter ? 1 : -1;
  61.                      }
  62.                   } else {
  63.                      int thumbX = this.this$0.getThumbRect().x;
  64.                      if (!this.this$0.slider.getInverted()) {
  65.                         direction = this.currentMouseX < thumbX ? -1 : 1;
  66.                      } else {
  67.                         direction = this.currentMouseX < thumbX ? 1 : -1;
  68.                      }
  69.                   }
  70.                   break;
  71.                case 1:
  72.                   if (this.this$0.getThumbRect().isEmpty()) {
  73.                      int scrollbarCenter = sbSize.height / 2;
  74.                      if (!this.this$0.slider.getInverted()) {
  75.                         direction = this.currentMouseY < scrollbarCenter ? 1 : -1;
  76.                      } else {
  77.                         direction = this.currentMouseY < scrollbarCenter ? -1 : 1;
  78.                      }
  79.                   } else {
  80.                      int thumbY = this.this$0.getThumbRect().y;
  81.                      if (!this.this$0.slider.getInverted()) {
  82.                         direction = this.currentMouseY < thumbY ? 1 : -1;
  83.                      } else {
  84.                         direction = this.currentMouseY < thumbY ? -1 : 1;
  85.                      }
  86.                   }
  87.             }
  88.  
  89.             this.this$0.scrollDueToClickInTrack(direction);
  90.             Rectangle r = this.this$0.getThumbRect();
  91.             if (!r.contains(this.currentMouseX, this.currentMouseY) && this.shouldScroll(direction)) {
  92.                this.this$0.scrollTimer.stop();
  93.                this.this$0.scrollListener.setDirection(direction);
  94.                this.this$0.scrollTimer.start();
  95.             }
  96.  
  97.          }
  98.       }
  99.    }
  100.  
  101.    public boolean shouldScroll(int direction) {
  102.       Rectangle r = this.this$0.getThumbRect();
  103.       if (this.this$0.slider.getOrientation() == 1) {
  104.          if (this.this$0.slider.getInverted() ? direction < 0 : direction > 0) {
  105.             if (r.y + r.height <= this.currentMouseY) {
  106.                return false;
  107.             }
  108.          } else if (r.y >= this.currentMouseY) {
  109.             return false;
  110.          }
  111.       } else if (this.this$0.slider.getInverted() ? direction < 0 : direction > 0) {
  112.          if (r.x + r.width >= this.currentMouseX) {
  113.             return false;
  114.          }
  115.       } else if (r.x <= this.currentMouseX) {
  116.          return false;
  117.       }
  118.  
  119.       if (direction > 0 && this.this$0.slider.getValue() + this.this$0.slider.getExtent() >= this.this$0.slider.getMaximum()) {
  120.          return false;
  121.       } else {
  122.          return direction >= 0 || this.this$0.slider.getValue() > this.this$0.slider.getMinimum();
  123.       }
  124.    }
  125.  
  126.    public void mouseDragged(MouseEvent e) {
  127.       Rectangle scrollTrackRect = this.this$0.getScrollTrackRect();
  128.       int thumbMiddle = 0;
  129.       if (this.this$0.slider.isEnabled()) {
  130.          this.currentMouseX = e.getX();
  131.          this.currentMouseY = e.getY();
  132.          if (BasicSliderUI.access$isDragging(this.this$0)) {
  133.             switch (this.this$0.slider.getOrientation()) {
  134.                case 0:
  135.                   int halfThumbWidth = this.this$0.getThumbRect().width / 2;
  136.                   int thumbLeft = e.getX() - this.offset;
  137.                   int trackLeft = scrollTrackRect.x + this.this$0.trackBuffer;
  138.                   int trackRight = scrollTrackRect.x + (scrollTrackRect.width - 1) - this.this$0.trackBuffer;
  139.                   thumbLeft = Math.max(thumbLeft, trackLeft - halfThumbWidth);
  140.                   thumbLeft = Math.min(thumbLeft, trackRight - halfThumbWidth);
  141.                   this.this$0.setThumbLocation(thumbLeft, this.this$0.getThumbRect().y);
  142.                   thumbMiddle = thumbLeft + halfThumbWidth;
  143.                   this.this$0.slider.setValue(this.valueForXPosition(thumbMiddle));
  144.                   break;
  145.                case 1:
  146.                   int halfThumbHeight = this.this$0.getThumbRect().height / 2;
  147.                   int thumbTop = e.getY() - this.offset;
  148.                   int trackTop = scrollTrackRect.y + this.this$0.trackBuffer;
  149.                   int trackBottom = scrollTrackRect.y + (scrollTrackRect.height - 1) - this.this$0.trackBuffer;
  150.                   thumbTop = Math.max(thumbTop, trackTop - halfThumbHeight);
  151.                   thumbTop = Math.min(thumbTop, trackBottom - halfThumbHeight);
  152.                   this.this$0.setThumbLocation(this.this$0.getThumbRect().x, thumbTop);
  153.                   thumbMiddle = thumbTop + halfThumbHeight;
  154.                   this.this$0.slider.setValue(this.valueForYPosition(thumbMiddle));
  155.                   break;
  156.                default:
  157.                   return;
  158.             }
  159.  
  160.          }
  161.       }
  162.    }
  163.  
  164.    public void mouseMoved(MouseEvent e) {
  165.    }
  166.  
  167.    public int valueForYPosition(int yPos) {
  168.       int minValue = this.this$0.slider.getMinimum();
  169.       int maxValue = this.this$0.slider.getMaximum();
  170.       Rectangle trackRect = this.this$0.getScrollTrackRect();
  171.       int trackLength = trackRect.height - this.this$0.trackBuffer * 2;
  172.       int trackTop = trackRect.y + this.this$0.trackBuffer;
  173.       int trackBottom = trackRect.y + (trackRect.height - 1) - this.this$0.trackBuffer;
  174.       int value;
  175.       if (yPos <= trackTop) {
  176.          value = this.this$0.slider.getInverted() ? minValue : maxValue;
  177.       } else if (yPos >= trackBottom) {
  178.          value = this.this$0.slider.getInverted() ? maxValue : minValue;
  179.       } else {
  180.          int distanceFromTrackTop = yPos - trackTop;
  181.          int valueRange = maxValue - minValue;
  182.          double valuePerPixel = (double)valueRange / (double)trackLength;
  183.          int valueFromTrackTop = (int)Math.round((double)distanceFromTrackTop * valuePerPixel);
  184.          value = this.this$0.slider.getInverted() ? minValue + valueFromTrackTop : maxValue - valueFromTrackTop;
  185.       }
  186.  
  187.       return value;
  188.    }
  189.  
  190.    public int valueForXPosition(int xPos) {
  191.       int minValue = this.this$0.slider.getMinimum();
  192.       int maxValue = this.this$0.slider.getMaximum();
  193.       Rectangle trackRect = this.this$0.getScrollTrackRect();
  194.       int trackLength = trackRect.width - this.this$0.trackBuffer * 2;
  195.       int trackLeft = trackRect.x + this.this$0.trackBuffer;
  196.       int trackRight = trackRect.x + (trackRect.width - 1) - this.this$0.trackBuffer;
  197.       int value;
  198.       if (xPos <= trackLeft) {
  199.          value = this.this$0.slider.getInverted() ? maxValue : minValue;
  200.       } else if (xPos >= trackRight) {
  201.          value = this.this$0.slider.getInverted() ? minValue : maxValue;
  202.       } else {
  203.          int distanceFromTrackLeft = xPos - trackLeft;
  204.          int valueRange = maxValue - minValue;
  205.          double valuePerPixel = (double)valueRange / (double)trackLength;
  206.          int valueFromTrackLeft = (int)Math.round((double)distanceFromTrackLeft * valuePerPixel);
  207.          value = this.this$0.slider.getInverted() ? maxValue - valueFromTrackLeft : minValue + valueFromTrackLeft;
  208.       }
  209.  
  210.       return value;
  211.    }
  212.  
  213.    // $FF: synthetic method
  214.    BasicSliderUI$TrackListener(BasicSliderUI this$0) {
  215.       this.this$0 = this$0;
  216.    }
  217. }
  218.